
raise ImportError, 'this is historical: don't use this module'

from whiff import gateway
from whiff.middleware import misc

class navigator(misc.utility):
    def __init__(self):
        pass

    def __call__(self, env, start_response):
        # sectionizer, etc was saved by getSectionPosition in env previously
        sectionizer = env["sectionizer"]
        filename = env["filename"]
        filelocator = sectionizer.filenameToLocator[filename]
        sectionizer.assignJsNames(env)
        sectionizer.markFocusedFiles(filename)
        sectionizer.getTemplateInfos(env)
        focusLocation = sectionizer.filenameToLocator[filename]
        # create js functions for each file
        lf = sectionizer.fileDictionaries.items()
        lf.sort()
        jsFunctions = []
        for (locator, d) in lf:
            expander = self.expander(sectionizer, locator, env, d, focusLocation)
            jsFunctions.append(expander)
            collapser = self.collapser(sectionizer, locator, env, d, focusLocation)
            jsFunctions.append(collapser)
        alljsFunctions = "\n".join(jsFunctions)
        # defer the declaration of the functions to the end of the response
        self.appendLocal("expandList", alljsFunctions, env)
        # add calls to expand the focus elements from root down
        expandlocator = filelocator
        expandcalls = []
        expandcalls.append( self.expandCall(sectionizer, expandlocator, env) )
        while expandlocator:
            expandlocator = expandlocator[:-1]
            expandcalls.append( self.expandCall(sectionizer, expandlocator, env) )
        expandcalls.reverse()
        # defer expand calls to onload function
        allexpandCalls = "\n".join(expandcalls)
        self.appendLocal("onloadList", allexpandCalls, env)
        # generate the root div to expand using javascript
        rootdict = sectionizer.fileDictionaries[ () ]
        rootdivId = rootdict["divId"]
        rootdivhtml = '<div id="%s"> * </div>' % rootdivId
        return self.deliver_page(rootdivhtml, env, start_response)
    
    def expander(self, sectionizer, locator, env, d, focusLocation, content=None, functionName=None, otherJs=None):
        "expand javascript function declaration for location"
        from whiff.middleware import setInnerHtml
        if content is None:
            content = self.expandedContent(sectionizer, locator, env, d, focusLocation)
        if functionName is None:
            functionName = d["expFn"]
        if otherJs is None:
            # add collapse calls for all children
            children = d["children"]
            collapseKids = [ self.collapseCall(sectionizer, kid, env) for kid in children ]
            otherJs = ";\n".join(collapseKids)
        elementName = d["divId"]
        app = setInnerHtml.__middleware__(elementName=elementName, page=content)
        setOperation = self.param_value(app, env)
        return "function %s () {\n %s; %s; \n}" % (functionName, setOperation, otherJs)
    def expandedContent(self, sectionizer, locator, env, d, focusLocation):
        filename = sectionizer.locatorToFilename[locator]
        info = sectionizer.locatorToTemplateInfo[locator]
        title = info["title"].strip()
        children = d["children"]
        colfn = d["colFn"]
        # if not focused, make the title into a link
        if not locator==focusLocation:
            url = info["url"]
            title = '<a href="%s" title="%s">%s</a>' % (url, title, title)
        action = "*"
        if children:
            action = '<a href="javascript:%s()">&mdash;</a>' % ( colfn, )
        indent = len(locator)*10
        L = []
        L.append('<div style="white-space:pre; padding-left:%spx">%s %s</div>' % (indent, action, title))
        # add divs for children
        children.sort()
        for childlocator in children:
            cd = sectionizer.fileDictionaries[childlocator]
            childDivId = cd["divId"]
            L.append('<div id="%s"> *child* </div>' %(childDivId,))
        return "\n".join(L)
    def collapsedContent(self, sectionizer, locator, env, d, focusLocation):
        # should refactor...
        filename = sectionizer.locatorToFilename[locator]
        info = sectionizer.locatorToTemplateInfo[locator]
        title = info["title"].strip()
        children = d["children"]
        expfn = d["expFn"]
        # if not focused, make the title into a link
        if not locator==focusLocation:
            url = info["url"]
            title = '<a href="%s" title="%s">%s</a>' % (url, title, title)
        action = "*"
        if children:
            action = '<a href="javascript:%s()">+</a>' % ( expfn, )
        indent = len(locator)*10
        result = '<div style="white-space:pre; padding-left:%spx">%s %s</div>' % (indent, action, title)
        return result
    def collapser(self, sectionizer, locator, env, d, focusLocation):
        "collapse javascript function declaration for location"
        content = self.collapsedContent(sectionizer, locator, env, d, focusLocation)
        functionName = d["colFn"]
        #pr "for", locator, "collapser fn name is", functionName
        result = self.expander(sectionizer, locator, env, d, focusLocation, content=content, functionName=functionName, otherJs="")
        #pr "collapser code is\n", result
        return result
    def expandCall(self, sectionizer, locator, env):
        "js call to expand the location"
        dictionary = sectionizer.fileDictionaries[ locator ]
        expandFn = dictionary["expFn"]
        return expandFn+"();"
    def collapseCall(self, sectionizer, locator, env):
        "js call to expand the location"
        dictionary = sectionizer.fileDictionaries[ locator ]
        collapseFn = dictionary["colFn"]
        return collapseFn+"();"
    
    def appendLocal(self, name, value, env):
        resourcePath = ["local", name, []]
        L = gateway.getResource(env, resourcePath, [])
        L.append(value)
        resourcePath = ["local", name]
        gateway.putResource(env, resourcePath, L)

__middleware__ = navigator
